package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.user.UserRelationConstants;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        Map<String, Boolean> map = new HashMap<>();
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            map.put("isfollow", false);
            map.put("islike", false);
            map.put("isunlike", false);
            map.put("iscollection", false);
            return ResponseResult.okResult(map);
        } else {
            boolean flag1 = true;
            ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
            if (entry==null){
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "获取行为实体信息失败");
            }

            Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(entry.getId()));
            ApLikesBehavior likeEntry = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (likeEntry!= null) {
                flag1 = false;
            }

            map.put("islike", flag1);
            boolean flag2 = true;
            ApUnlikesBehavior unlikesBehaviorEntry = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if (unlikesBehaviorEntry != null) {
                flag2 = false;
            }

            map.put("isunlike", flag2);
            boolean flag3 = true;
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            if (apCollection != null) {
                flag3 = false;
            }
            map.put("iscollection", flag3);
            boolean flag4 = true;
            Double score = redisTemplate.opsForZSet()
                    .score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorApUserId()));
            if (score!=null){
                flag4 = false;
            }
            map.put("isfollow", flag4);
        }

        return ResponseResult.okResult(map);
    }
}
